//
//  AppendAppCell.swift
//  HLDesktop
//
//  Created by 黄龙 on 2023/8/29.
//

import UIKit

class AppendAppCell: UITableViewCell {
    var icoLogo: UIImageView!
    var titleLabel: UILabel!
    var addBtn:UIButton!
    public var didTapAddBlock: os_block_t? //点击添加
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = .clear
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        icoLogo = UIImageView(frame: CGRect(x: 20, y: 10, width: 40, height: 40))
        contentView.addSubview(icoLogo)
        icoLogo.layer.cornerRadius = 10
        icoLogo.clipsToBounds = true
        icoLogo.image = UIImage(named: defaultIcoLogo)
        
        addBtn = UIButton(frame: CGRect(x: bounds.size.width - 40, y: bounds.size.height/2-20, width: 40, height: 40))
        contentView.addSubview(addBtn)
        addBtn.setImage(UIImage(named: "cell_add@2x.png"), for: .normal)
//        addBtn.setTitle("+", for: .normal)
//        addBtn.titleLabel?.font = .systemFont(ofSize: 14)
//        addBtn.setTitleColor("d779ff".color, for: .normal)
//        addBtn.layer.cornerRadius = 10
//        addBtn.layer.borderWidth = 1
//        addBtn.layer.borderColor =   "d779ff".color.cgColor
        addBtn.addActionWithBlock { sender in
            self.didTapAddBlock?()
        }
        
        let iLeft = 20.0+40.0+10.0
        titleLabel = UILabel(frame: CGRectMake(iLeft, 0, bounds.size.width - iLeft - 15 - 16 - 10, bounds.size.height))
        contentView.addSubview(titleLabel)
        titleLabel.textAlignment = .left
//        titleLabel.lineBreakMode = .byTruncatingHead
//        titleLabel.adjustsFontSizeToFitWidth = true
        titleLabel.textColor =  .label//.secondaryLabel
        titleLabel.font = .systemFont(ofSize: 16)
        titleLabel.text = ""
        titleLabel.numberOfLines = 2
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.addBtn.frame = CGRect(x: bounds.size.width - 40, y: bounds.size.height/2-20, width: 40, height: 40)
        let iLeft = 20.0+40.0+10.0
        self.titleLabel.frame = CGRectMake(iLeft, 0, bounds.size.width - iLeft - 45 , bounds.size.height)
    }

    func showTitle(title:String){
        let arr = title.components(separatedBy: "&|&")//至少需&|&title=&|&icon=
        if arr.count >= 3 {
            var sTitle = ""
            var sIcon = ""
            for perData in arr{
                if perData.hasPrefix("title="){
                    sTitle = (perData as NSString).substring(from: 6) as String
                }else if perData.hasPrefix("icon="){
                    sIcon = (perData as NSString).substring(from: 5) as String
                }
            }
            
            if sTitle.isEmpty && sIcon.isEmpty{//只要有一个有问题就说明出错了，显示错误信息
                self.icoLogo.image = UIImage(named: defaultIcoLogo)
                self.titleLabel.text = "unknown"
            }else{
                self.configTitle(sTitle.trimSpace())
                self.icoLogo.setImageWithStr(imgurl: sIcon.trimSpace(),defName: defaultIcoLogo)
            }
        }else{
            self.icoLogo.image = UIImage(named: defaultIcoLogo)
            self.titleLabel.text = "unknown"
        }
    }
    
    
    func configTitle(_ tmpTitle:String){
        let titleArr = AiScreenFunc.splitToTitleAndSub(title: tmpTitle)
        if titleArr.count>1{
            let newTitle = String(format: "%@\n%@", titleArr[0],titleArr[1])
            let myMutableString = NSMutableAttributedString(
                        string: newTitle,
                        attributes: [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 16),
                                     NSAttributedString.Key.foregroundColor:UIColor.label])
            let range_1:NSRange = (newTitle as NSString).range(of:titleArr[1])
            myMutableString.addAttributes([NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14),
                                           NSAttributedString.Key.foregroundColor:UIColor.secondaryLabel], range: range_1)
            self.titleLabel.attributedText = myMutableString
        }else{
            self.titleLabel.font = .systemFont(ofSize: 16)
            self.titleLabel.textColor =  .label
            self.titleLabel.text = tmpTitle
        }
    }
    
}
